# Http Interface Library
# for use with Robot Framework 
#
# Seamless Distribution AB, 2010
#

import urllib

class HttpInterfaceLibrary:
    """ This library is mainly meant for usage when testing services with pure
        http interfaces like smsapp and ussdapp.
    
    """

    def __init__(self, url):
        self.url = url

    def sendSMSAppRequest(self, sender, receiver, message):
        """
        Sends a http request to smsapp with the command specified.

            Parameters:
            sender - The sender's MSISDN
	    receiver - The destination receiver sms shortcode
            message - The sms content that should be sent as sms

            Returns:
            No return, but can use other function to fetch sms kannel log
        """
        fullURL = self.url + "/smsapp?from=%s&to=%s&msg=%s" % (sender, receiver, message)

        f = urllib.urlopen(fullURL)

        return f.read()
        
#    def sendSMSAppRequest(self, sender, message):
#        """
#        Sends a http request to smsapp with the command specified.
#
#            Parameters:
#            sender - The sender's MSISDN
#            message - The sms content that should be sent as sms
#
#            Returns:
#            No return, but can use other function to fetch sms kannel log
#        """
#        fullURL = self.url + "/smsapp?from=%s&msg=%s" % (sender,message)

#        f = urllib.urlopen(fullURL)

#        return f.read()
    
    def sendUSSDAppRequest(self, sender, command, sessionId=None):
        """ Sends a http request to ussdapp with the command specified.

            Parameters:
            sender - The sender's MSISDN
            command - The number or string that should be sent as USSD (e.g. "200" or "4")
            sessionId - Unique session id as issued by USSDGW (Optional)

            Returns:
            (string) The message returned from ussdapp (like service menu, or "Input PIN")
        """
        if sessionId:
            params = ('src',sender),('sessionid', sessionId),('cmd',command)
        else:    
            params = ('src',sender),('cmd',command)

        urlParams = urllib.urlencode(params)

        fullUrl = self.url + "/cc/CC?" + urlParams
        print "Sending request: " + fullUrl
        f = urllib.urlopen(fullUrl)
        response = f.read()
        # Strip first part of message which includes "USSDGW<code>"

        # TBD - Implement support for protocol version 2
        
        info = response[:10]
        message = response[10:]
        return message,info

    def sendUSSDAppRequest_2_params(self, sender, command, p1, p2, sessionId=None):
        """ Sends a http request to ussdapp with the command specified.

            Parameters:
            sender - The sender's MSISDN
            command - The number or string that should be sent as USSD (e.g. "200" or "4")
            sessionId - Unique session id as issued by USSDGW (Optional)

            Returns:
            (string) The message returned from ussdapp (like service menu, or "Input PIN")
        """
        if sessionId:
            params = ('src',sender),('sessionid', sessionId),('cmd',command),('p1',p1),('p2',p2)
        else:    
            params = ('src',sender),('cmd',command),('p1',p1),('p2',p2)

        urlParams = urllib.urlencode(params)

        fullUrl = self.url + "/cc/CC?" + urlParams
        print "Sending request: " + fullUrl
        f = urllib.urlopen(fullUrl)
        response = f.read()
        # Strip first part of message which includes "USSDGW<code>"

        # TBD - Implement support for protocol version 2
        
        info = response[:10]
        message = response[10:]
        return message,info

    def sendUSSDAppRequest_3_params(self, sender, command, p1, p2, p3, sessionId=None):
        """ Sends a http request to ussdapp with the command specified.

            Parameters:
            sender - The sender's MSISDN
            command - The number or string that should be sent as USSD (e.g. "200" or "4")
            sessionId - Unique session id as issued by USSDGW (Optional)

            Returns:
            (string) The message returned from ussdapp (like service menu, or "Input PIN")
        """
        if sessionId:
            params = ('src',sender),('sessionid', sessionId),('cmd',command),('p1',p1),('p2',p2),('p3',p3)
        else:    
            params = ('src',sender),('cmd',command),('p1',p1),('p2',p2),('p3',p3)

        urlParams = urllib.urlencode(params)

        fullUrl = self.url + "/cc/CC?" + urlParams
        print "Sending request: " + fullUrl
        f = urllib.urlopen(fullUrl)
        response = f.read()
        # Strip first part of message which includes "USSDGW<code>"

        # TBD - Implement support for protocol version 2
        
        info = response[:10]
        message = response[10:]
        return message,info

    def sendUSSDAppRequest_4_params(self, sender, command, p1, p2, p3, p4, sessionId=None):
        """ Sends a http request to ussdapp with the command specified.

            Parameters:
            sender - The sender's MSISDN
            command - The number or string that should be sent as USSD (e.g. "200" or "4")
            sessionId - Unique session id as issued by USSDGW (Optional)

            Returns:
            (string) The message returned from ussdapp (like service menu, or "Input PIN")
        """
        if sessionId:
            params = ('src',sender),('sessionid', sessionId),('cmd',command),('p1',p1),('p2',p2),('p3',p3),('p4',p4)
        else:    
            params = ('src',sender),('cmd',command),('p1',p1),('p2',p2),('p3',p3),('p4',p4)

        urlParams = urllib.urlencode(params)

        fullUrl = self.url + "/cc/CC?" + urlParams
        print "Sending request: " + fullUrl
        f = urllib.urlopen(fullUrl)
        response = f.read()
        # Strip first part of message which includes "USSDGW<code>"

        # TBD - Implement support for protocol version 2
        
        info = response[:10]
        message = response[10:]
        return message,info

    def sendUSSDAppRequest_5_params(self, sender, command, p1, p2, p3, p4, p5, sessionId=None):
        """ Sends a http request to ussdapp with the command specified.

            Parameters:
            sender - The sender's MSISDN
            command - The number or string that should be sent as USSD (e.g. "200" or "4")
            sessionId - Unique session id as issued by USSDGW (Optional)

            Returns:
            (string) The message returned from ussdapp (like service menu, or "Input PIN")
        """
        if sessionId:
            params = ('src',sender),('sessionid', sessionId),('cmd',command),('p1',p1),('p2',p2),('p3',p3),('p4',p4),('p5',p5)
        else:    
            params = ('src',sender),('cmd',command),('p1',p1),('p2',p2),('p3',p3),('p4',p4),('p5',p5)

        urlParams = urllib.urlencode(params)

        fullUrl = self.url + "/cc/CC?" + urlParams
        print "Sending request: " + fullUrl
        f = urllib.urlopen(fullUrl)
        response = f.read()
        # Strip first part of message which includes "USSDGW<code>"

        # TBD - Implement support for protocol version 2
        
        info = response[:10]
        message = response[10:]
        return message,info

    def getUrl(self, url=None):
        """ Simple keyword for doing a http request
        """
        
        if url==None:
            url = self.url

        f = urllib.urlopen(url)
        return f.read()

